package org.example.processor;

import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;

import java.util.HashMap;
import java.util.Map;

public class MyBeanFacotryPostProcessor implements BeanFactoryPostProcessor {
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (beanFactory instanceof DefaultListableBeanFactory) {
            DefaultListableBeanFactory listableBeanFactory = (DefaultListableBeanFactory) beanFactory;

            RootBeanDefinition beanDefinition = new RootBeanDefinition();
            beanDefinition.setBeanClassName("org.example.dao.impl.UserDaoImpl");
            Map<String, String> properties = new HashMap<>();
            properties.put("tag", "由后置处理器 BeanFactoryPostProcessor 创建");
            beanDefinition.setPropertyValues(new MutablePropertyValues(properties));

            listableBeanFactory.registerBeanDefinition("userDao01", beanDefinition);
        }
    }
}
